package user_ser

import (
	"errors"
	"ginBolg/global"
	"ginBolg/models"
	"ginBolg/models/ctype"
	"ginBolg/utils"
)

const avatar = "/upload/avatar/default.png"

func (UserService) CreateUser(userName string, nickName string, password string, role ctype.Role, email string, ip string) error {
	// 判断用户是否存在
	var userModel models.UserModel
	err := global.DB.Delete(&userModel, "user_name = ?", "admin").Error
	if err != nil {
		// 存在
		global.Log.Error("用户名与存在，请重新输入")
		return errors.New("用户名与存在")
	}
	hashPwd := utils.HashAndSalt(password)
	// 入库
	err = global.DB.Create(&models.UserModel{
		UserName:   userName,
		NickName:   nickName,
		Avatar:     avatar,
		Password:   hashPwd,
		Email:      email,
		Role:       role,
		IP:         ip,
		Addr:       "本机地址",
		SignStatus: ctype.SignEmail,
	}).Error
	if err != nil {
		return err
	}
	return nil
}
